package com.filldream.fastboot.web.controller.common;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson2.JSONObject;
import com.filldream.fastboot.common.okutil.stock.cf.kline.GetKLineSTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.RepeatRequestAnnotation;
import com.filldream.fastboot.common.constant.enums.StockFstEnum;
import com.filldream.fastboot.common.constant.enums.StockMarketUtil;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.entity.db.StockBaseInfo;
import com.filldream.fastboot.common.okutil.stock.cf.CfKLineUtil;
import com.filldream.fastboot.common.okutil.stock.cf.kline.KLineDTO;
import com.filldream.fastboot.common.okutil.stock.cffex.GetStockFuturesSTO;
import com.filldream.fastboot.common.okutil.stock.cls.zdStatistics.ClsZdStatisticsUtil;
import com.filldream.fastboot.common.stockUtils.qt.entity.StockInfo;
import com.filldream.fastboot.common.stockUtils.qt.StockUtil;
import com.filldream.fastboot.common.util.FileIOUtil;
import com.filldream.fastboot.core.job.task.StockIndexTask;
import com.filldream.fastboot.core.manager.StockManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

@AllArgsConstructor
@Api(tags = "股票相关接口")
@RestController
@RequestMapping("/common/stock")
public class StockController {

    private final StockManager stockManager;

    @GetMapping(value = "/fuzzySearchStock")
    @ApiOperation(value = "根据股票名称/代码/拼音获取股票信息",notes = "根据股票名称获取股票代码")
    public R<List<StockBaseInfo>> getStockCodeByName(String name) {
        return stockManager.getList(name);
    }

    @GetMapping(value = "/getStockPrice")
    @ApiOperation(value = "获取股票实时价格",notes = "获取股票实时价格")
    public R<List<StockInfo>> getStockPrice(String codes) {
        return R.autoSuccess(StockUtil.getByCodes(codes));
    }

    @Log(value = "刷新股市指数")
    @GetMapping(value = "/refreshStockIndex")
    @ApiOperation(value = "刷新股市指数",notes = "刷新股市指数，发送到钉钉")
    @RepeatRequestAnnotation(overTime = 60 * 5,checkData = false,globalFlag = true)
    public B refreshStockIndex() {
        StockIndexTask.autoSendStockIndex();
        return B.create(ErrorCode.SUCCESS.reMsg(), "刷新成功，请关注群消息，5分钟内禁止再次获取");
    }

    @PostMapping(value = "/getKLine")
    @ApiOperation(value = "获取K线数据",notes = "获取K线数据")
    public R<JSONObject> getKQuotation(@RequestBody GetKLineSTO sto ) {
        return R.autoSuccess(StockFstEnum.getKLine( sto.getType(), sto.getSearchCode() ));
    }

    @GetMapping(value = "/V2/getKLine")
    @ApiOperation(value = "获取K线数据V2",notes = "获取K线数据V2")
    public R<KLineDTO> getKLine(GetKLineSTO sto ) {
        return R.autoSuccess( CfKLineUtil.getKLine(sto) );
    }

    /**
     * imgType: ZMTRJT  r
     * @param response
     * @param searchCode
     * @throws IOException
     * https://webquotepic.eastmoney.com/GetPic.aspx?imageType=ZMTRJT&id=
     */
    @GetMapping("/getMinImg")
    @ApiOperation(value = "获取及时分时图片")
    public void getMinImg(HttpServletResponse response,String searchCode) throws IOException {
        String id =  StockMarketUtil.getCfImgIdBySearchCode(searchCode);
        byte[] bytes = FileIOUtil.url2byte("https://webquotepic.eastmoney.com/GetPic.aspx?imageType=r&nid=" + id);
        ByteArrayInputStream fileInputStream = IoUtil.toStream(bytes);
        response.setHeader("Content-Disposition", "attachment; filename=" + searchCode + "_" + System.currentTimeMillis() + ".png");
        StreamUtils.copy(fileInputStream,response.getOutputStream());
    }

    @GetMapping(value = "/V1/getStockIndexFuturesByDay")
    @ApiOperation(value = "获取股指期货数据",notes = "获取股指期货数据")
    public R<Object> getStockIndexFuturesByDay(GetStockFuturesSTO sto) {
        return R.autoSuccess( stockManager.getStockIndexFutures(sto) );
    }

    @GetMapping(value = "/V2/zdStatistics")
    @ApiOperation(value = "获取财联社涨跌分布统计",notes = "获取财联社涨跌分布统计")
    public Object getZdStatistics() {
        return ClsZdStatisticsUtil.getClsZdData();
    }

    @GetMapping(value = "/V2/getUpDownAnalysis")
    @ApiOperation(value = "获取财联社涨跌幅榜单",notes = "获取财联社涨跌幅榜单")
    public Object getUpDownAnalysis(String type) {
        return ClsZdStatisticsUtil.getUpDownAnalysis(type);
    }

}
